package com.ticket.sass.admin.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.dto.input.operationlog.OperationLogListInput;
import com.ticket.sass.admin.mapper.OperationLogMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.OperationLogEntity;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-07
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLogEntity> implements BaseService<OperationLogEntity> {

    public IPage<OperationLogEntity> iPage(OperationLogListInput input) {
        Page<OperationLogEntity> page = new Page<>(input.getPageNum(), input.getPageSize());
        QueryWrapper<OperationLogEntity> queryWrapper = buildQueryWrapper(entityClass, input);
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("is_success", Boolean.TRUE);
        queryWrapper.in(Objects.nonNull(input.getModules()) && input.getModules().length > 0, "module", input.getModules());
        queryWrapper.in(CollectionUtil.isNotEmpty(input.getAdminIds()), "admin_id", input.getAdminIds());
        
        // 如果有效日期范围不为空，则添加范围查询条件
        if (input.getStartDate() != null && input.getEndDate() != null) {
            queryWrapper.between("created_at", input.getStartDate(), input.getEndDate());
        }
        
        return page(page, queryWrapper);
    }
}
